package com.yzy.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yzy.vo.ListDetailDTO;
import com.yzy.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.math.BigDecimal;
import java.net.URI;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

class Vo{
    private String name;
}
/**
* @description: TODO
* @author 杨镇宇
* @date 2022/4/7 23:30
* @version 1.0
*/
@Controller
public class HtmlThymeleafController {

    public static void main(String[] args) {
        String [] param = new String[]{"name#姓名#0"};
        Vo vo = new Vo();
        checkNull(param,vo);
    }
    public static String checkNull(String [] inputs,Vo vo){
        try{
            Class<? extends Vo> clazz = vo.getClass();
            for (String input:inputs){
                String[] data = input.split("#");
                char [] get = data[0].toCharArray();
                get[0]-=32;
                String msg = data[1];
                switch (data[2]){
                    //0:String、1:Integer、2:BigDecimal、3:Short、4:Date、5:Long
                    case "0":
                        //String
                        String invoke =(String)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke == null || "".equals(invoke)){
                            return msg+"不能为空";
                        }
                        break;
                    case "1":
                        //Integer
                        Integer invoke1 =(Integer)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke1 == null ){
                            return msg+"不能为空";
                        }
                        break;
                    case "2":
                        //BigDecimal
                        BigDecimal invoke2 =(BigDecimal)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke2 == null ){
                            return msg+"不能为空";
                        }
                        break;
                    case "3":
                        //Short
                        Short invoke3 =(Short)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke3 == null){
                            return msg+"不能为空";
                        }
                        break;
                    case "4":
                        //Date
                        Date invoke4 =(Date)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke4 == null ){
                            return msg+"不能为空";
                        }
                        break;
                    case "5":
                        //Long
                        Long invoke5 =(Long)clazz.getMethod("get"+String.valueOf(get)).invoke(vo);
                        if (invoke5 == null ){
                            return msg+"不能为空";
                        }
                        break;
                    default:
                        return "校验类型输入错误";
                }
            }
            return "";
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }

    }
    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("name","age");
        return "main";
    }
    @Autowired
    @Qualifier("yzyTemplate")
    private RestTemplate restTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping("/server")
    public void server() throws JsonProcessingException {
        //传参
        String requestBody = "{\n" +
                "    \"businessInvoiceId\":\"4688e4567d6b4f94be7ba00335fee058\"\n" +
                "}";
        Map<String,Object> jsonObject = JSONObject.parseObject(requestBody);
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //整理动态URL
        Map<String, String> urlParams = new HashMap();
        urlParams.put("v", "invoice");
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://127.0.0.1:62371/{v}/detail");
        URI uri = builder.buildAndExpand(urlParams).toUri();
        HttpEntity<Object> requestEntity = new HttpEntity(jsonObject, headers);
        ParameterizedTypeReference<Result<ListDetailDTO>> responseType = new ParameterizedTypeReference<Result<ListDetailDTO>>() {
        };
        ResponseEntity<Result<ListDetailDTO>> resp = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, responseType);
        System.out.printf(objectMapper.writeValueAsString(resp.getStatusCodeValue())); //200
        System.out.printf(objectMapper.writeValueAsString(resp.getStatusCode())); //OK
        System.out.printf(objectMapper.writeValueAsString(resp.getBody())); //返回内容
    }
}
